<?php
defined('IN_PHPCMS') or exit('No permission resources.');

pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);
class business  {
	private $db;
	private $business_db,$signed_db;
	private $buy_product_db; 
	private $business_data;
	private $messages_db;
	private $content_db;
	function __construct() {
		//取得进来的查看的会员信息
		$phpcms_auth = param::get_cookie('auth');
		$auth_key = md5(pc_base::load_config('system', 'auth_key').str_replace('7.0' ,'8.0',$_SERVER['HTTP_USER_AGENT']));
		list($userid, $password)= explode("\t", sys_auth($phpcms_auth, 'DECODE', $auth_key));
		$this->userdb = pc_base::load_model('member_model');
		$this->memberinfo = $this->userdb->get_one(array('userid'=>$userid));
		
		//取得公司用户副表信息
		$uid = intval($_GET['uid'])>0 ? intval($_GET['uid']) : $_POST['uid'];
		if($uid<1) {return ;}
		$this->business_db = pc_base::load_model('business_model');
		$this->member_db = pc_base::load_model('member_model');
		
		$this->business_data = $this->business_db->get_one('userid='.$uid);
		$this->uid = $this->business_data['userid'];
		//取得公司用户主表信息
		if($this->uid){
		$this->business_user = $this->member_db->get_one('userid='.$this->uid);
		}
		$this->db = pc_base::load_model('content_model');
	}
	//首页
	public function init() {
	
		if(isset($_GET['siteid'])) {
			$siteid = intval($_GET['siteid']);
		} else {
			$siteid = 1;
		}
		$siteid = $GLOBALS['siteid'] = max($siteid,1);
		//SEO
		$SEO = seo($siteid);
		$sitelist  = getcache('sitelist','commons');
		$default_style = $sitelist[$siteid]['default_style'];
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		include template('content','index',$default_style);
	}
	//商家首页
	function index(){
		$cityid  = $_SESSION['cityid'];
		$this->messages_db = pc_base::load_model('messages_model');
		$b_data = $this->business_data;
		$table_pre = $this->messages_db->db_tablepre;
		$member_business_db = $table_pre.'member_business';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
		$memberinfo = $this->memberinfo;
		if($_GET['messageid'] && $this->business_data['userid']==$memberinfo['userid']){
			$con = ' and a.messageid='.$_GET['messageid'];
		}
		$sql = 'SELECT a.content,a.addtime,a.messageid,a.replyid,a.name, b.content as rcontent,b.replyid as rid 
FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_business_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.intval($_GET['uid']).$con.' order by a.messageid desc ';

			$sql_count = 'select count(a.messageid) as num FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_business_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.intval($_GET['uid']).$con;
		
		$sql .= " LIMIT $offset,$pagesize ";
		//echo $sql;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		
		$total = $total[0]['num'];
		$memberinfo = $this->memberinfo;
		//print_r($message_data);exit;
		$reply = $this->business_data['userid'] == $memberinfo['userid'] ? 1:0;
		include template('business','business_default');
	}
	//商家简介
	function about_us(){
		$b_data = $this->business_data;
		include template('business','about_us');
	}
	//商家产品
	function product(){
		$b_data = $this->business_data;
		include template('business','product');
	}
	function product_dt(){
		$b_data = $this->business_data;
		$id = intval($_GET['id']);
		$this->content_db = pc_base::load_model('content_model');
		$tablename = $this->db->table_name = $this->db->db_tablepre.'product';
		$r = $this->content_db->get_one(array('id'=>$id));
	
		if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
		
		$this->content_db->table_name = $tablename.'_data';
		$r2 = $this->db->get_one(array('id'=>$id));
		$rs = $r2 ? array_merge($r,$r2) : $r;
		$rs['thumb'] = get_thumb_url($rs['thumb']);
	
		include template('business','product_dt');
	}
	//商家公告
	function notice(){
		$b_data = $this->business_data;
		$username = $this->business_user['username'];
		include template('business','notice');
	}
	//商家建材杂淡
	function decoration_topic(){
		$b_data = $this->business_data;
		$username = $this->business_user['username'];
		include template('business','decoration_topic');
	}
	function contact(){
		$this->messages_db = pc_base::load_model('messages_model');
		$b_data = $this->business_data;
		$table_pre = $this->messages_db->db_tablepre;
		$member_business_db = $table_pre.'member_business';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
		$memberinfo = $this->memberinfo;
		if($_GET['messageid'] && $this->business_data['userid']==$memberinfo['userid']){
			$con = ' and a.messageid='.$_GET['messageid'];
		}
		$sql = 'SELECT a.content,a.addtime,a.messageid,a.replyid,a.name, b.content as rcontent,b.replyid as rid 
FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_business_db.' AS c ON c.userid = a.uuid
WHERE a.uuid ='.intval($_GET['uid']).$con.' order by a.messageid desc ';

			$sql_count = 'select count(a.companyid) as num FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_business_db.' AS c ON c.userid = a.uuid
WHERE a.uuid ='.intval($_GET['uid']).$con;
		
		$sql .= " LIMIT $offset,$pagesize ";
		//echo $sql;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		
		$total = $total[0]['num'];
		$memberinfo = $this->memberinfo;
		
		$reply = $this->business_data['userid']==$memberinfo['userid'] ? 1:0;
	//	print_r($message_data);exit;	
		$pages2 = pages($total, $page, $pagesize);
		include template('business','contact');
	}
	

	
	//商家公告详细
	function notice_detail(){
		$b_data = $this->business_data;
		$id = intval($_GET['id']);
		$this->content_db = pc_base::load_model('content_model');
		$tablename = $this->db->table_name = $this->db->db_tablepre.'discount_notice';
		$r = $this->content_db->get_one(array('id'=>$id));
	
		if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
		
		$this->content_db->table_name = $tablename.'_data';
		$r2 = $this->db->get_one(array('id'=>$id));
		$rs = $r2 ? array_merge($r,$r2) : $r;
		include template('business','notice_detail');
	}
	//建材杂谈详细
	function decoration_topic_detail(){
		$b_data = $this->business_data;
		$id = intval($_GET['id']);
		$this->content_db = pc_base::load_model('content_model');
		$tablename = $this->db->table_name = $this->db->db_tablepre.'decoration_topic';
		$r = $this->content_db->get_one(array('id'=>$id));
	
		if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
		
		$this->content_db->table_name = $tablename.'_data';
		$r2 = $this->db->get_one(array('id'=>$id));
		$rs = $r2 ? array_merge($r,$r2) : $r;
		include template('business','decoration_topic_detail');
	}
	//留言咨询
	function message(){
		
		$this->messages_db = pc_base::load_model('messages_model');
		if(isset($_POST['dosubmit'])){
			$this->_session_start();
			if($_SESSION['code']!=$_POST['code']){
				showmessage('验证码输入不正确',HTTP_REFERER);
			}
			$username = trim($_POST['username']);

			$user = $this->userdb->get_one(array('username'=>$username));
		

			$memberinfo = $this->memberinfo;
			$userid = $memberinfo['userid'];
			//print_r($this->business_user);exit;
			$message_data = array(
				'content'=>$_POST['content'],
				'name'=>$_POST['name'],
				'phone'=>$_POST['phone'],
				'userid'=>$userid['userid'],
				'post_userid'=>$this->memberinfo['userid'],
				'addtime'=>strtotime("now")
			);
			//print_r($message_data);exit;
			$this->messages_db->insert($message_data);
			showmessage('你的提问已成功请交，我们会尽快复您的问题',HTTP_REFERER);
		}
		$table_pre = $this->messages_db->db_tablepre;
		$member_business_db = $table_pre.'member_business';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
		$memberinfo = $this->memberinfo;
		if($_GET['messageid'] && $this->business_data['userid']==$memberinfo['userid']){
			$con = ' and a.messageid='.$_GET['messageid'];
		}
	$sql = 'SELECT a.content,a.addtime,a.messageid,a.replyid,a.name, b.content as rcontent,b.replyid as rid 
FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_company_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.intval($_GET['userid']).$con.' order by a.messageid desc ';

			$sql_count = 'select count(a.messageid) as num FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_company_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.intval($_GET['userid']).$con;
		
		$sql .= " LIMIT $offset,$pagesize ";
		//echo $sql;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		
		$total = $total[0]['num'];
		$memberinfo = $this->memberinfo;
		
		$reply = $this->company_data['userid']==$memberinfo['userid'] ? 1:0;
		print_r($message_data);exit;	
		$pages2 = pages($total, $page, $pagesize);
		//echo '<pre>';
		//print_r($message_data);exit;
		$b_data = $this->business_data;
		include template('company','message');
	}
	//公司回复留言
	function reply_message(){
		$this->messages_db = pc_base::load_model('messages_model');
		if(isset($_POST['dosubmit'])){
			$memberinfo = $this->memberinfo;
			$userid = $memberinfo['userid'];
			$message_data = array(
				'content'=>$_POST['content'],
				'userid'=>$userid,
				'replyid'=>$_POST['messageid'],
				'addtime'=>strtotime("now")
			);
			$result = $this->messages_db->insert($message_data);
			if($result==1){
				$r = array('id'=>$_POST['messageid'], 'content'=>$_POST['content']);
				echo json_encode($r); 
			}else{
				echo 0;
			}
		}
	}
	
	/**
	 * 
	 * 删除留言
	 */
	function del_message(){
		$messageid = intval($_GET['messageid']);
		if($messageid<1) return ;
		//echo $messageid;exit;
		$this->messages_db = pc_base::load_model('messages_model');
		$this->messages_db->delete('messageid='.$messageid);
		$this->messages_db->delete('replyid='.$messageid);
		showmessage('删除成功',HTTP_REFERER); 
	}
	//公司评价
	function appraise(){
		$this->signed_db = pc_base::load_model('signed_model');
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
	
		$sql = 'SELECT *  FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->userid.' and inputtime <> \'0\' order by signedid desc ';

		$sql_count = 'select count(signedid) as num FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->userid.' and inputtime  <> \'0\' order by signedid desc ';
		
		$sql .= " LIMIT $offset,$pagesize ";
		
		$this->signed_db->query($sql);
		$pj_data = $this->signed_db->fetch_array();
		//print_r($pj_data);exit;
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		$total = $total[0]['num'];
		$pagesbar = pages($total, $page, $pagesize);
		
		$b_data = $this->business_data;
		include template('company','appraise');
	}
	
	private function _session_start() {
		$session_storage = 'session_'.pc_base::load_config('system','session_storage');
		pc_base::load_sys_class($session_storage);
	}
}
?>